package spring.main;

import java.util.List;
import javax.swing.DefaultListModel;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import org.springframework.social.twitter.api.*;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import ventana.Twiteo;
import ventana.VentanaPrincipal;

public class Lector {

    
    private List<TwitterProfile> amigos;
    private List<String> nombresAmigos = null;
    String consumerKey = "BUb0lJE80kD5eM2C7xVdLw"; // The application's consumer key
    String consumerSecret = "0reVv7jE1vm516jYY2TZflS1qoY9u46s6J6bOnpnt4"; // The application's consumer secret
    String accessToken = Sesion.getSesion().getAccessToken(); // The access token granted after OAuth authorization
    String accessTokenSecret = Sesion.getSesion().getAccessTokenSecret(); // The access token secret granted after OAuth authorization
    private Twitter twitter = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
    
    public void actualizar(DefaultListModel modeloTweets, DefaultListModel modeloAmigos) {
        modeloTweets.clear();
        TwitterProfile profile = twitter.userOperations().getUserProfile();
        List<Tweet> tweets = twitter.timelineOperations().getHomeTimeline();
        amigos = twitter.friendOperations().getFriends(profile.getScreenName());
        
   
        //List<Tweet> tweets = (List<Tweet>)response;
        //System.out.println(amigos);
//       for (int i = 0; i < amigos.size(); i++) {
//            nombresAmigos.add(amigos.get(i).getName());
//        }

        for (Tweet t : tweets) {
            modeloTweets.addElement("@"+t.getFromUser());
            modeloTweets.addElement(t.getText());
        }
        for (TwitterProfile amigo : amigos) {
            modeloAmigos.addElement(amigo.getScreenName());
        }
    }

    public int actualizarStatus(String status){
        int resultado=1;
        twitter.timelineOperations().updateStatus(status);
        
        return resultado;
    }
    public String getNombreUsuario(){
        TwitterProfile profile = twitter.userOperations().getUserProfile();
        return profile.getScreenName();
    }
    
    public void tweetearAmigo(String usuario) {
        Twiteo ventanaTwiteo = new Twiteo(usuario);
        ventanaTwiteo.setVisible(true);
    }

    public void verTweetsAmigo(DefaultListModel modeloTweets, DefaultListModel modeloAmigos,String amigo){
        modeloTweets.clear();
        List<Tweet> tweets = twitter.timelineOperations().getUserTimeline(amigo);
        for (Tweet t : tweets) {
            modeloTweets.addElement("@"+t.getFromUser());
            modeloTweets.addElement(t.getText());
        }
    }
    public static void seguirAutorizacion(String codigo, OAuthService service, Token requestToken) {
        Verifier verifier = new Verifier(codigo);
        Token accessToken = service.getAccessToken(requestToken, verifier); // the requestToken you had from step 2
        Sesion.getSesion().setAccessToken(accessToken.getToken());
        Sesion.getSesion().setAccessTokenSecret(accessToken.getSecret());
        
//        //String PROTECTED_RESOURCE_URL = "https://api.twitter.com/1/statuses/update.json";
//        OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.twitter.com/1/account/verify_credentials.xml");
//        //OAuthRequest request = new OAuthRequest(Verb.POST,PROTECTED_RESOURCE_URL );
//        //request.addBodyParameter("status", "this is sparta! *");
//        service.signRequest(accessToken, request); // the access token from step 4
//        Response response = request.send();
//        System.out.println(response.getBody());

        Lector lector = new Lector();
        
        VentanaPrincipal vent = new VentanaPrincipal(lector.getNombreUsuario());
        vent.setVisible(true);

    }

    public Lector() {
    }

    public Lector(Twitter twitter) {
        this.twitter = twitter;
    }
}
